/*Ingredients*/
/*Purpose : Query for project C.2*/
/*PIC : Archie Anugrah. Project C.2*/
/*Nature : Static Class*/
/*Status : Completed*/

#include "Queryarc.h"
using namespace std;

Queryarc::Queryarc() //default constructor
{
}

// Function : C.2. c. Display the top five selling menu items by revenue regardless of category
// Type	: Main Function
// PIC : Archie
// Status : Complete
void Queryarc::DisplayTopFiveMenuItems(vector<Order_Item> order_itemsVector, vector<Menu_item> menu_itemsVector)
{
	 map<int, double> MenuSalesTotal; // map of MenuItem_id and their total sales

	//iterate the Order_Item vector. process all the menu_id and quantity to compute sales
	for(int i = 0; i < order_itemsVector.size();i++)
	{
		int curr_menu_id = order_itemsVector[i].getMenuItemID(); //variable filled with current menu_id
		int curr_quantity = order_itemsVector[i].getQuantity(); //variable filled with current quantity
		double curr_price = ShowFindUpdate::MenuItemMenu_IdToPrice(menu_itemsVector,curr_menu_id); //variable filled with current price
		MenuSalesTotal[curr_menu_id]+=(curr_price*curr_quantity); //add the sales
	}

	cout<<"Top 5 Menu : \n\n";
	print_menu_sales(MenuSalesTotal,menu_itemsVector); //print the top 5 sales
}

// Function : Print sales map
// Type	: Support Function
//author : DNRN edited by archie
//reference : http://www.cplusplus.com/forum/beginner/5217/
void Queryarc::print_menu_sales(const map<int, double>& m, vector<Menu_item> menu_itemsVector) {
	map<int, double>::iterator it, itr, max;
	map<int, double> ret;
	ret = m;

	for(int i=0;i<5;i++) //only fot the top five --archie
	{
		itr = ret.begin(), max = ret.begin(), it = ret.begin();
		int j = ret.size();
		while ( j > 0 ) {
			if ( itr->second > it->second) {
				max = itr;
				it = itr;
			}
			++itr;
			--j;
		}
		string curr_name = ShowFindUpdate::MenuItemMenu_IdToName(menu_itemsVector,max->first); //variable filled with current name -- Archie add
		cout << max->first << ":" << curr_name << "\t" << max->second << endl; //print id:name:totalprice -- archie
		ret.erase(max);
	}
}

// Function : C.2. d. Display the order with the largest tab price
// Type	: Main Function
// PIC : Archie
// Status : Complete
void Queryarc::DisplayTopOrder(vector<Order_Item> order_itemsVector, vector<Menu_item> menu_itemsVector)
{
	map<int, double> OrderIDTotal; // map of OrderID and their total sales

	//iterate the Order_Item vector. process all the menu_id and quantity to compute sales
	for(int i = 0; i < order_itemsVector.size();i++)
	{
		int curr_order_id = order_itemsVector[i].getOrderId(); //variable filled with current order_id
		int curr_menu_id = order_itemsVector[i].getMenuItemID(); //variable filled with current menu_id
		int curr_quantity = order_itemsVector[i].getQuantity(); //variable filled with current quantity
		double curr_price = ShowFindUpdate::MenuItemMenu_IdToPrice(menu_itemsVector,curr_menu_id); //variable filled with current price
		OrderIDTotal[curr_order_id]+=(curr_price*curr_quantity); //add the sales
	}

	//search the largest value on the map
	double maxValue = 0; //variable to compare the max value
	map<int, double>::iterator maxValuePos; //variable to contain max value position
	for(map<int, double>::iterator it = OrderIDTotal.begin(); it != OrderIDTotal.end();it++) //search for the max value
	{
		if (it->second > maxValue)
		{
			maxValue = it->second;
			maxValuePos = it;
		}
	}

	cout<<"Top order :\n"<< maxValuePos->first << " : " << maxValuePos->second << "\n"; //show the value
}

// Function : Team problem : Delete a menu item from a category. Return true if erase succeeded
// Logic : Search for the searched menu_items vector. Delete the one that found.
//         Not delete other vector, assumpt that the one that need to be deleted only the menu_item vector.
// Type	: Main Function
// PIC : Archie
// Status : Complete
bool Queryarc::DeleteMenuItemfromCate(vector<Menu_item> &menu_itemsVector,string category_id, int menu_id)
{
	for(vector<Menu_item>::iterator it = menu_itemsVector.begin(); it != menu_itemsVector.end();it++) //search for the max value
	{
		//if found, delete the record
		if(((*it).get_cat_id() == category_id) && ((*it).get_menu_item_id() == menu_id))
		{
			menu_itemsVector.erase(it);
			return true;
		}
	}
	return false;	
}
